import Vue from 'vue'
import Vuex from 'vuex'
import {
  dayConsume,
  weekData,
  countMonth,
  countYear
} from '@/utils/statistics.js'
import {
  weekArray,
  lastWeekArray,
  year,
  month
} from "@/utils/fromDate.js"
import {
  getStorage,
  setStorage,
  clearStorage
} from '@/utils/storage.js'
Vue.use(Vuex)
let tally = uniCloud.importObject('tally')
const store = new Vuex.Store({
  state: {
    tallyList: [],
    weekConut: {
      data: {},
      expends: [],
      incomes: [],
    },
    lastWeekConut: {
      expends: [],
      incomes: [],
    },
    monthConut: {
      data: {},
      expends: [],
      incomes: [],
    },
    yearConut: {
      data: {},
      expends: [],
      incomes: []
    }
  },
  mutations: {
    GET_YEAR_TALLY(state, data) {
      // console.log(data);
      state.yearConut.data = data
      state.yearConut.expends = countYear(data, 0)
      state.yearConut.incomes = countYear(data, 1)
    },
    GET_MONTH_TALLY(state, data) {
      state.monthConut.data = data
      state.monthConut.expends = countMonth(data, 0)
      state.monthConut.incomes = countMonth(data, 1)
    },
    GET_TALLY_LiST(state, data) {
      // console.log(data);
      state.tallyList = data
      // console.log(data);
    },
    GET_WEEK_TALLY(state, data) {
      console.log(data);
      state.weekConut.data = data
      state.weekConut.expends = weekData(data, 0)
      state.weekConut.incomes = weekData(data, 1)
    },
    GET_LASTWEEK_TALLY(state, data) {
      // console.log(data);
      state.lastWeekConut.expends = weekData(data, 0)
      state.lastWeekConut.incomes = weekData(data, 1)
      // console.log(weekData(data,1),"?????");
    },
  },
  actions: {
    // 获取tally的数据
    async getTallyList({
      commit
    }, date) {
      // console.log();
      let res = await tally.getMonthData(date.year, date.month)
      if (res.code == 200) {
        commit('GET_TALLY_LiST', dayConsume(res.data))
      }
    },
    // 删除一条tally数据
    async delTally({
      dispatch
    }, data) {
      let res = await tally.del(data.id)
      if (res.code == 200) {
        dispatch('getTallyList', data)
        clearStorage()
      }
    },
    // 增加一条tally
    async addTally({
      dispatch
    }, data) {
      let res = await tally.add(data)
      if (res.code == 200) {
        clearStorage()
        dispatch('getTallyList', {
          year: data.year,
          month: data.month
        })
      }
      return res
    },
    // 修改一条tally
    async updataTally({
      dispatch
    }, arrData) {
      clearStorage()
      let res = await tally.updata(arrData[0], arrData[1])
      if (res.code == 200) {
        dispatch('getTallyList', {
          year: arrData[1].year,
          month: arrData[1].month
        })
      }
      return res
    },
    // 获取一个周和上一个周的数据
    async getWeekTally({
      commit
    }) {
      console.log(weekArray,lastWeekArray);
      if (!getStorage('weekTally')) {
        await Promise.all([tally.getWeekData(weekArray), tally.getWeekData(lastWeekArray)]).then(value => {
          console.log(value);
          if (value[0].code == 200 && value[1].code == 200) {
            setStorage('weekTally', value)
            // 本周的数据
            commit('GET_WEEK_TALLY', value[0].data)
            // 上周的数据
            commit('GET_LASTWEEK_TALLY', value[1].data)
          }
        }).catch(e => {
          console.log(e);
        })
      } else {
        let value = getStorage('weekTally')
        console.log(value);
        commit('GET_WEEK_TALLY', value[0].data)
        // 上周的数据
        commit('GET_LASTWEEK_TALLY', value[1].data)
      }
    },
    // 获取本月的数据
    async getMonthTally({
      commit
    }) {
      if (!getStorage('monthTally')) {
        let res = await tally.getMonthData(year, month)
        if (res.code == 200) {
          setStorage('monthTally', res.data)
          commit('GET_MONTH_TALLY', res.data)
        }
      } else {
        let data = getStorage('monthTally')
        commit('GET_MONTH_TALLY', data)
      }
    },
    // 获取年的数据
    async getYearTally({
      commit
    }) {
      if (!getStorage('yearTally')) {
        let res = await tally.getYearData(year)
        if (res.code == 200) {
          setStorage('yearTally', res.data)
          commit('GET_YEAR_TALLY', res.data)
        }
      } else {
        let data = getStorage('yearTally')
        commit('GET_YEAR_TALLY', data)
      }
    }
  },
  getters: {
    weekExpend(state) {
      return state.weekConut.expends
    },
    weekIncome(state) {
      return state.weekConut.incomes
    },
    lastWeekExpend(state) {
      return state.lastWeekConut.expends
    },
    lastWeekIcome(state) {
      // console.log(state.lastWeekConut.incomes,"!!!!!");
      return state.lastWeekConut.incomes
    },
    weekCount(state) {
      return state.weekConut.expends.reduce((pre, cur) => {
        return pre += cur
      }, 0).toFixed(2)
    },
    weekContrast(state) {
      let week = state.weekConut.expends.reduce((pre, cur) => {
        return pre += cur
      }, 0).toFixed(2)
      let lastWeek = state.lastWeekConut.expends.reduce((pre, cur) => {
        return pre += cur
      }, 0).toFixed(2)
      if (week > lastWeek) {
        return `多${week-lastWeek}`
      } else if (week < lastWeek) {
        return `少${lastWeek-week}`
      } else {
        return '相等'
      }
    },
    monthExpend(state) {
      return state.monthConut.expends
    },
    monthIcome(state) {
      return state.monthConut.incomes
    },
    yearExpend(state) {
      return state.yearConut.expends
    },
    yearIcome(state) {
      return state.yearConut.incomes
    },
    monthCount(state) {
      // console.log(state.monthConut.expends);
      let money =state.monthConut.expends.reduce((pre, cur) => {
        return pre += cur
      }, 0)
      return money.toFixed(2)
    },
    yearCount(state) {
      let money = state.yearConut.expends.reduce((pre, cur) => {
        return pre += cur
      }, 0)
      return money.toFixed(2)
    },
  }
})
export default store
